package day04.text02;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class Service {

    public static void main(String[] args) throws IOException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InstantiationException {
        //读取properties的键值对
        BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\group5InClass27\\src\\day04\\text02\\network.properties"));

        Properties properties = new Properties();

        properties.load(br);

        br.close();


        ServerSocket serverSocket = new ServerSocket(8888);
        Socket accept = serverSocket.accept();
        InputStream inputStream = accept.getInputStream();
        //读取网络中的信息
        BufferedReader br1 = new BufferedReader(new InputStreamReader(inputStream));
        String b = "";
        String s;
        while ((s = br1.readLine()) != null) {
            b += s;
        }
        //通过网络获取的key来调用properties中的值
        String property = properties.getProperty(b);

        //加载对于网络创来的的class文件
        Class<?> aClass = Class.forName(property);

        //创新对象
        Object o = aClass.newInstance();
        //调用里面的run方法
        Method method = aClass.getMethod("run");
        String invoke = (String) method.invoke(o);
        System.out.println(invoke);

        br1.close();
        accept.close();
        serverSocket.close();
    }

}
